//
//  StoryDetailsTableViewController.m
//  PetTown
//
//  Created by lanou3g on 15/5/31.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "StoryDetailsTableViewController.h"
#import "StoryTableViewController.h"
#import "StoryDetailsCell.h"
#import "StoryDetails.h"
#import "StoryPage.h"
#import "StoryTableViewController.h"
#import "UIImageView+WebCache.h"
@interface StoryDetailsTableViewController ()
{
    StoryDetailsCell *cell;
    MBProgressHUD * HUD;
}

@end

@implementation StoryDetailsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.allDataArray = [[NSMutableArray alloc]init];
    self.title = _name;

    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    
    
    
#pragma mark---数据解析
    [self StoryDetailsJSon];
    //表头视图
    UIImageView * BTImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/3.0f)];
    
    BTImageV.backgroundColor = [UIColor orangeColor];
    
        [BTImageV sd_setImageWithURL:[NSURL URLWithString:_imgURL]];
    self.tableView.tableHeaderView = BTImageV;

    

    
    _b = 1;
    //下拉刷新
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //上啦刷新
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];

   }

//下拉刷新
- (void)headerRereshing{
    
    [self.tableView headerEndRefreshing];
}
//上拉刷新
-(void)footerRereshing{
    _b++;
    [self StoryDetailsJSon];
    [self.tableView footerEndRefreshing];

}


#pragma mark---数据解析
- (void)StoryDetailsJSon
{
    HUD = [[MBProgressHUD alloc] initWithView:self.tableView];
    [self.tableView addSubview:HUD];

    HUD.labelText = @"正在加载";
    [HUD show:YES];
    
    NSString * str = [NSString stringWithFormat:StoryDetailsCommentAPI,_ppid,[NSString stringWithFormat:@"%d",_b]];
  
 
    NSURL * url = [NSURL URLWithString:str];
    
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
    
    __weak StoryDetailsTableViewController * storyDetailsTVC = self;
   
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data == nil) {
            return ;
        }
     
        NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       
        NSMutableDictionary * dic1 = dic[@"data"];
        NSArray * array = dic1[@"list"];
        for (NSDictionary * modelDic in array) {
            StoryDetails * storyDetails = [[StoryDetails alloc]init];
            [storyDetails setValuesForKeysWithDictionary:modelDic];
            [storyDetailsTVC.allDataArray addObject:storyDetails];

            
        }
        [self.tableView reloadData];

    }];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    //清除缓存
    SDImageCache * sdI = [SDImageCache sharedImageCache];
    [sdI clearDisk];
    [sdI clearMemory];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    
         return _allDataArray.count;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 //cell重用
    cell = [[StoryDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"storyCell"];

        StoryDetails * storyDetail = _allDataArray[indexPath.row];
        cell.storyDetails = storyDetail;
    //大菊花缓冲
    //HUD = nil;
    [MBProgressHUD hideHUDForView:self.tableView animated:YES];
//    }
    
return cell;
}







//设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString * string = @"所有评论";
    
    return string;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
